qui(
    function(require, exports, module) {

        NewsDetails.Grid = function(options) {
            this.dao = NewsDetails.Dao;
            this.config = NewsDetails.Config;
            this.util = NewsDetails.Util;
            this.options = options || {};
            this.construct = options.construct || function() {
                this.init();
            };
            this.construct.call(this);

        };

        Com.inherits(NewsDetails.Grid, Ancestor.Grid).extend({
            init: function() {
                var self = this;
                var $container = self.options.$container;
                self.$grid = $container.find('table.grid');
                self.perpareData();
                self.bindBasicEvent();
            },

            perpareData: function() {
                var self = this;
                var username = $("div.user a.name").html();
                var requestData = {url: NewsDetails.urlConfig.machinelistUrl, type: "get",
                                                    params: {type: 0, area: 0, username: username, _: new Date().getTime(), from_client: true }
                                                  };
                self.dao.redirect({"request": JSON.stringify(requestData), "timeSign": new Date().getTime()}, function (data) {
                    var parentJDom = $("select[name=serverRegion]");
                    var hasDataRegion = [];

                    $("<option>", {value: "all", html: "全部"}).appendTo(parentJDom);

                    if (data != null && data.response != null && data.response.list) {
                        $.each(data.response.list, function (index, obj) {
                            if (obj[1] >= 0 && obj[1] <= 2) {
                                if (hasDataRegion.indexOf(obj[1]) == -1) {
                                    hasDataRegion.push(obj[1]);
                                }

                                NewsDetails.Config.IP_LINK_AREA[obj[0]] = obj[0] + "[" + NewsDetails.Config.REGION_LIST[obj[1]] + "]";
                            }
                        });
                        $.each(hasDataRegion, function (i, element) {
                            $("<option>", {
                                value: element,
                                html: NewsDetails.Config.REGION_CH_LIST[element]
                            }).appendTo(parentJDom);
                        })
                    } else {
                        console.log("wait to do");
                    }
                });
            },

            bindBasicEvent: function() {
                var self = this;
                var $container = self.options.$container;
                var $form = $container.find('form[name="basicFrm"]');
                self.bindFormEvent($form);
                $container.find('.query-btn').on('click.query', function() {
                    self.query();
                    return false;
                });

				$("[type=reset]").click(function(){
                    $("form[name=basicFrm]")[0].reset();
                    $("select[name=serverRegion]").change();
				});

                $("select[name=serverRegion]").bind("change", function (event) {
                    var selectJDom = $("#filter_source_ip");
                    selectJDom.html("");
                    var username = $("div.user a.name").html();
                    var requestData = {url: NewsDetails.urlConfig.machinelistUrl, type: "get",
                                                        params: {type: 0, area: 0, username: username, _: new Date().getTime(), from_client: true }
                                                      };

                    self.dao.redirect({"request": JSON.stringify(requestData), "timeSign": new Date().getTime()}, function(data) {
                        var responseData = data;
                        var eqVal = event.currentTarget.value == "all" ? null : parseInt(event.currentTarget.value);
                        $("<option value='all'>全部</option>").appendTo(selectJDom);
                        if (responseData != null && responseData.response != null && responseData.response.list != null) {
                            $.each(responseData.response.list, function (i, obj) {
                                if ((eqVal == null && obj[1] >= 0 && obj[1] <= 2) || eqVal == obj[1]) {
                                    $("<option>", {
                                        value: obj[0],
                                        html: NewsDetails.Config.IP_LINK_AREA[obj[0]]
                                    }).appendTo(selectJDom);
                                }
                            })
                        }
                    });

                });
                $("select[name=serverRegion]").change();
			},

			bindFormEvent: function($form) {
                var self = this;
                $form.find('input[name="startDate"]').datetimepicker({
                    onClose: function(selectedDate) {
                        $form.find('input[name="startDate"]').datepicker("option", "minDate", selectedDate);
                    },
                    yearRange: '2010:+0'
                });
                $form.find('input[name="endDate"]').datetimepicker({
                    onClose: function(selectedDate) {
                        $form.find('input[name="endDate"]').datepicker("option", "maxDate", selectedDate);
                    },
                    yearRange: '2010:+0'
                });

			},

            run: function(options, callback) {
                var self = this;
                var gridConfig = self.getGridConfig();

                self.echoQuery();

                options = $.extend(true, {
                    width: 1850,
                   // multiselect: true,
                    multiselectWidth: 30,
                    shrinkToFit: false,
                    // idPrefix: 'Video-',
                    autowidth: false
                }, gridConfig, options);
                self.initGrid.call(this, options, callback);
            },

            initGrid: function(options, callback) {
                var self = this;
                var params = self.getParam();

                var gridConfig = self.getGridConfig();
                var $gridContainer = self.options.$container;
                var $pager = $gridContainer.find('div.pager');
                var $grid = $gridContainer.find('table.grid');

                //params.rowNum=10;

                options = $.extend(true, {
                    pager: $pager,
                    mtype: 'post',

                   postData: $.extend(true, {}, params),

                }, gridConfig, options);

                // $grid.jqGrid('scroll', {});

                //console.log(options	);

                $grid.jqGrid(options);

            },

            getParam: function() {
                var self = this;
                var $container = self.options.$container;

                var $grid = self.$grid;
                var params = {};
                var $form = $container.find('form[name="basicFrm"]');

                var $elements = $.makeArray($form.prop('elements')).filter(function(item) {
					return false;
                });

				var checkboxVal = [];
                var name;
                var key;
                $.each($elements, function() {
                    name = this.name;
                    if (name) {
						if(name=='repeat_info')
						{
							if(this.checked)
							{
								checkboxVal.push($(this).val());
							}
						}
						else
						{
							if($(this).val()){
								params[name] = $(this).val();
							}
						}
                    }

                });
				if(checkboxVal.length!==0){
					params['repeat_info']=JSON.stringify(checkboxVal);//params['repeat_info'].join(',');
				}
                return params;
            },

            query: function() {
                var self = this;
                var $container = self.options.$container;
                var $grid = self.$grid;
                self.overridePostData($grid);
                var params = self.getParam();
                $grid.jqGrid('setGridParam', {
                    page: 1,
                    datatype: "json",
                    postData: params
                }).trigger('reloadGrid');
            },

			del : function(channel_id,ablum_id){
                var self = this;

                if(window.confirm('是否确认删除该专辑信息？')){

                    return true;
                }
            }


        });

    }
);